import React from "react";
import logo from '../../../images/logo.png';
import frame from "../../../style/frame.less";
import * as Http from "../../utils/http";

class Login extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            protocol: false,
            doSubmit: new Http.URL('/login.do'),
            error: ''
        }
    }

    componentDidMount() {

    }

    doSubmit = () => {
        const {doSubmit, account, password} = this.state;

        doSubmit
            .setParameters(
                {account, password}
            )
            .post()
            .then(data => {
                if (data.code == 200) {
                    localStorage.setItem('access_token', data.body.access_token);
                    localStorage.setItem('refresh_token', data.body.refresh_token);
                    localStorage.setItem('username', data.body.username);
                    localStorage.setItem('currentAuthority', data.body.currentAuthority);

                    alert("登录成功");
                    window.location.href = "/";
                } else {
                    alert(data.msg);
                }
            })
            .then(() => {
                //do something
            })
    };

    render() {
        return (
            <div className={frame.registerContentCld}>
                <div style={{height: '10vh'}}></div>
                <div style={{width: '368px', margin: 'auto'}}>
                    <div className={frame.title}>
                        <a href='/'>
                            <img src={logo} alt="logo"/>
                            <span className={frame.fontRegister}>****Cloud</span>
                        </a>
                    </div>
                    <div className={frame.intro}>
                        为企业级用户&nbsp;|&nbsp;个人消费者用户助力
                    </div>
                    <div className={frame.loginCon}>
                        <div className={frame.userIput}>
                            <input id='account' placeholder='用户名' type='text'
                                   onChange={e => this.setState({account: e.target.value})}>
                            </input>
                            <input id='password' placeholder='密码' type='password'
                                   onChange={e => this.setState({password: e.target.value})}>
                            </input>
                        </div>
                    </div>
                    {/*<div className={frame.autoLogin}>
                        <label style={{cursor: 'pointer'}}>
                            <span><input type='checkbox' style={{marginTop: '5px', float: 'left'}}></input></span>
                            <span style={{color: 'rgba(0,0,0,.65);'}}>自动登录</span>
                        </label>
                        <a href="/forgetPassword">忘记密码</a>
                    </div>*/}
                    <div className={frame.submitBtn}>
                        <button type='submit' onClick={this.doSubmit}>
                            <span>登录</span>
                        </button>
                    </div>
                    <div className={frame.otherLogin}>
                        <div>
                            <a href="/register">注册账户</a>
                        </div>
                    </div>
                    <div style={{height: '100px'}}>
                    </div>
                </div>
            </div>
        );
    }
}

export default Login;
